# -*- coding: utf-8 -*-

# Author: Samuel Santos
# Data: 18/05/2012
# samuel.santos@ccc.ufcg.edu.br

import pygame, sys
from pygame.locals import*
import random

#define colors
black = (0,0,0)
white = (255,255,255)
red   = (255,0,0)

#this class represents the ball

class Block(pygame.sprite.Sprite):

	def __init__(self, color, width, height):
		pygame.sprite.Sprite.__init__(self)
		
		self.image = pygame.image.load("player.png").convert()
		self.image.set_colorkey(white)
		
		# Fetch the rectangle object that has the
		# dimensions of the image
		# updae the position of this object by
		# setting the values
		# of rect.x and rect.y
		
		self.rect = self.image.get_rect()
		
#initialize pygame		
pygame.init()
		
# Set the height and width of the screen
screenWidth = 600
screenHeight = 400
screen = pygame.display.set_mode([screenWidth, screenHeight])

#this is a list of 'sprites'
# Each block in the program is added to this list
# the list is manegend by a class called 'RenderPlain'
blockList = pygame.sprite.RenderPlain()

#this is a list of every sprite.
#All blocks and the player block as well.
allSpriteList = pygame.sprite.RenderPlain()

#set positions
posx = screenWidth /2 #centerx
posy = screenHeight / 2 #centery
blockWidth = 10
blockHeight = 10


#create blocks
block1 = Block(white,blockWidth, blockHeight)
block2 = Block(white,blockWidth, blockHeight)
block3 = Block(white,blockWidth, blockHeight)
block4 = Block(white,blockWidth, blockHeight)


block1.rect.x = posx
block1.rect.y = posy

block2.rect.x = posx + 10
block2.rect.y = posy

block3.rect.x = posx + 20
block3.rect.y = posy

block4.rect.x = posx + 30
block4.rect.y = posy



#add in bockList
blockList.add(block1)
blockList.add(block2)
blockList.add(block3)
blockList.add(block4)

allSpriteList.add(block1)
allSpriteList.add(block2)
allSpriteList.add(block3)
allSpriteList.add(block4)


# Funcoes de movimentos

#@param block - um objeto do tipo pygame.rect
def moveRigth(block):
	block.rect.x += 1

#@param block - um objeto do tipo pygame.rect
def moveLeft(block):
	block.rect.x -= 1

#@param block - um objeto do tipo pygame.rect
def moveUp(block):
	block.rect.y -= 1

#@param block - um objeto do tipo pygame.rect
def moveDown(block):
	block.rect.y += 1

#movimenta a lista de block na direcao informada
#@param direction - uma string "d", "u", "r", "l" indicando a dire��o.
#@param blockList - uma lista de retangulos
def move(direction, blockList):
	#pegamos a cabe�a da lista "headList"
	headList = blockList[0]
	
	#movemos apenas a cabe�a da lista na direcao indicada.
	if direction == "r":
		moveRigth(headList)
	elif direction == "l":
		moveLeft(headList)
	elif direction == "u":
		moveUp(headList)
	elif direction == "d":
		moveDown(headList)
		
	# Deslocamos os block dentro da lista para a posicao
	# onde estava a cabe�a da lista antes de ser movida
	# IDEIA DO ALGORITMO:
	# lista = ['block1', "block2", "block3"]
	# head = bloco1 , ou seja, o primeiro elemento da lista! 
	# move(direction, head) , mova a cabe�a da lista!
	# block3 = block2 , desloque os demais elementos...
	# block2 = block1
	# block1 = head
	# lista = ['head', "block1", "block2"] , a lista moveu!!!
	
	for i in range(1, len(blockList)):
		blockList[-i].rect.x = blockList[-i-1].rect.x
		blockList[-i].rect.y = blockList[-i-1].rect.y
		
	blockList[0].rect.x = headList.rect.x
	blockList[0].rect.y = headList.rect.y

	
#direction to move...
# "r" - right, "l" - left, "u" - up e "d" - down
direction = "l"

#time
clock = pygame.time.Clock()
		
while True:
	
	#verificar o evento 'sair' e finaliza.
	for event in pygame.event.get():
		if event.type == QUIT:
			exit()

	#verifica as teclas pressionadas
	pressedKeys = pygame.key.get_pressed()
	
	#movimenta na posi��o da tecla pressionada
	if pressedKeys[K_RIGHT]:
		direction = "r"
	if pressedKeys[K_LEFT]:
		direction = "l"
	if pressedKeys[K_UP]:
		direction = "u"
	if pressedKeys[K_DOWN]:
		direction = "d"

	#movendo a lista de blocos na direcao indicada
	move(direction, blockList)
	
	#draw all the spites
	#allSpritesList.draw(screen)
	
	#time
	clock.tick(20)
	
	#atualiza a tela em cada itera��o
	pygame.display.flip()
	

